package com.bbx.taxi.client.widget.Dailog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.text.GetChars;
import android.text.Html;
import android.text.SpannableString;
import android.view.Window;
import android.view.Window.Callback;

import com.bbx.api.gclient.GMsg;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.UnprocessedList;
import com.bbx.api.sdk.model.passanger.Return.Jmsg.Msgs;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.MainActivity;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity2;
import com.bbx.taxi.client.Activity.Order.OrderUnprocessed;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Activity.login.LoginActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Extra.ExamineMsg;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;
import com.bbx.taxi.client.DB.UnprocessedList.UnprocessedListDB;
import com.bbx.taxi.client.Service.LoginService;
import com.bbx.taxi.client.Util.DataCleanManager;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.ToCity;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickSingleListener;
import com.bbx.taxi.client.widget.Dailog.MyBusyDailog;
import com.bbx.taxi.client.widget.Dailog.MyPayOrderDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;
import com.bbx.taxi.client.widget.Dailog.MyPayOrderDailog.onClickPayListener;

/**
 * 消息过来的弹窗类型
 * 
 */
public class MessageDialog {
	     public static int max_dialog=15;
	     public static Map<String,Integer> map_dialog=new HashMap<String, Integer>();    


	/**
	 * 客服取消
	 */
	public synchronized static void onKfCancelDialog(final Activity context,
			final Msgs.Data msgs_data) {
		MyTypeDailog canceldialog = new MyTypeDailog(context);
		canceldialog.setModel(MyTypeDailog.TYPE_CANCEL, msgs_data);
	}

	/**
	 * 改派
	 */
	public synchronized static void onGaipaiDialog(final Activity context,
			final Msgs.Data msgs_data) {
		MyTypeDailog gaipaidialog = new MyTypeDailog(context);
		gaipaidialog.setModel(MyTypeDailog.TYPE_GAIPAI, msgs_data);
	}

	/**
	 * 下车支付
	 */
	public synchronized static void onOffCarDialog(final Activity context,
			final Msgs.Data msgs_data) {
		if (msgs_data != null) {
			new AsyncTask<Void, Void, Void>() {
				MyOrderDB db_myorder;
				OfficialOrderList mOfficialOrderList;
				String order_id;
				String start_city;
				String end_city;
				MyPayOrderDailog payorder_dialog;

				@Override
				protected Void doInBackground(Void... arg0) {
					order_id = msgs_data.getOrder_id();
					db_myorder = new MyOrderDB(context);// 我的进行中的订单数据库
					mOfficialOrderList = db_myorder.onReadDB(context,
							order_id);
					if(mOfficialOrderList!=null){
					start_city = ToCity.getCity(mOfficialOrderList
							.getOrderDetail().getStart().getAddress());
					end_city = ToCity.getCity(mOfficialOrderList
							.getOrderDetail().getEnd().getAddress());
					}
					return null;
				}

				@Override
				protected void onPostExecute(Void result) {
					if(mOfficialOrderList!=null){
					payorder_dialog = new MyPayOrderDailog(context);
					payorder_dialog.setOrderId(order_id);
					payorder_dialog.setMoney(FormatUtil.onFormatPrice(Integer
							.parseInt(mOfficialOrderList.getPrice())));
					payorder_dialog.setContent(String.format(
							context.getString(R.string.order),
							DateFormat.onDateRemove_ss(mOfficialOrderList
									.getAppoint_time())
									+ "\n"
									+ start_city
									+ "到" + end_city));
					if (payorder_dialog != null && !payorder_dialog.isShowing()) {
						payorder_dialog.show();
					}
					payorder_dialog
							.setOnClickPayListener(new onClickPayListener() {
								@Override
								public void onClickPay() {
									Intent intent = new Intent(context,
											OrderEvaluationActivity2.class);
									intent.putExtra(OrderMsg.order_id_finish,
											order_id);
									intent.putExtra(OrderMsg.finish_type,
											OrderMsg.finish_ing);
									context.startActivity(intent);
								}
							});
					}
					super.onPostExecute(result);
				}

			}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}

	/**
	 * 繁忙
	 */
	public synchronized static void onBusyDialog(final Activity context,
			final Msgs.Data msgs_data) {
		new AsyncTask<Void, Void, Void>() {
			OfficialOrderList mOfficialOrderList;
			String start_city;
			String end_city;

			@Override
			protected Void doInBackground(Void... arg0) {
				if (msgs_data != null && msgs_data.getText() != null) {
					MyOrderingDB db_myordering = new MyOrderingDB(context);// 我的进行中的订单数据库
					mOfficialOrderList = db_myordering.onReadDB_ing(context,
							msgs_data.getOrder_id());
					if(mOfficialOrderList!=null){
					start_city = ToCity.getCity(mOfficialOrderList
							.getOrderDetail().getStart().getAddress());
					end_city = ToCity.getCity(mOfficialOrderList
							.getOrderDetail().getEnd().getAddress());
					}
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				if(mOfficialOrderList!=null){
				MyBusyDailog busyDailog = new MyBusyDailog(context);
				if (msgs_data != null && msgs_data.getText() != null) {
					busyDailog.setTitle(msgs_data.getText());
					busyDailog.setContent(DateFormat
							.onDateRemove_ss(mOfficialOrderList
									.getAppoint_time())
							+ "\n" + start_city + "→" + end_city);
					if (busyDailog != null && !busyDailog.isShowing()) {
						busyDailog.show();
					}
			      }
				}
				super.onPostExecute(result);
			}

		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

	}

	/**
	 * 重登
	 */
	public synchronized static void onReloginDialog(final Activity context) {
		context.stopService(new Intent(context, LoginService.class));
		MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getString(R.string.mydailog_title));
		dialog.setContent(context.getString(R.string.mydailog_content_login));
		dialog.setLeftButtonText(context.getString(R.string.mydailog_exit));
		dialog.setRightButtonText(context.getString(R.string.mydailog_relogin));
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		dialog.setOnClickLeftListener(new onClickLeftListener() {
			@Override
			public void onClickLeft() {
				DataCleanManager.cleanDatabases(context);
				DataCleanManager.cleanSharedPreference(context);
				context.finish();
			}
		});
		dialog.setOnClickRightListener(new onClickRightListener() {

			@Override
			public void onClickRight() {
				Intent intent = new Intent();
				intent.setClass(context, LoginActivity.class);
				context.startActivity(intent);
			}
		});
	}

	/**
	 * 重登2
	 */
	public synchronized static void onReloginDialog2(final Activity context) {
		context.stopService(new Intent(context, LoginService.class));
		MyAlertDailog dialog = new MyAlertDailog(context);
		Resources res = context.getResources();
		dialog.setTitle(res.getString(R.string.mydailog_title));
		dialog.setContent(res.getString(R.string.mydailog_content_login));
		dialog.setLeftButtonText(res.getString(R.string.mydailog_exit));
		dialog.setRightButtonText(res.getString(R.string.mydailog_relogin));
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		dialog.setOnClickLeftListener(new onClickLeftListener() {
			@Override
			public void onClickLeft() {
				context.stopService(new Intent(context, LoginService.class));
				DataCleanManager.cleanDatabases(context);
				DataCleanManager.cleanSharedPreference(context);
				Intent intent = new Intent(context, MainActivity.class);
				intent.putExtra(MainMsg.extra_quit, MainMsg.INTENT_QUIT);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
				context.overridePendingTransition(0, 0);
				context.finish();
				context.overridePendingTransition(0, 0);
			}
		});
		dialog.setOnClickRightListener(new onClickRightListener() {

			@Override
			public void onClickRight() {
				Intent intent = new Intent();
				intent.setClass(context, LoginActivity.class);
				context.startActivity(intent);
			}
		});
	}

	/** 审核通过跟不拒绝 */
	public synchronized static void onUnprocessed(final Activity context,
			final Msgs.Data msgs_data) {
		if (msgs_data != null) {
			final String order_id = msgs_data.getOrder_id();
			MyOrderDB db_order = new MyOrderDB(context);
			final OfficialOrderList mOfficialOrderList = db_order.onReadDB(context, order_id);
			Resources res = context.getResources();
			final MyAlertDailog dialog = new MyAlertDailog(context);
			dialog.setTitle(res.getString(R.string.mydailog_title));
			String result = msgs_data.getResult();
			if (result.equals(Value.MSG_RESULT_PASS)) {
				dialog.setContent(res.getString(R.string.msg_unprocessed_pass));
				dialog.setLeftButtonText(res.getString(R.string.mydailog_know));
				dialog.setRightButtonText(res.getString(R.string.mydailog_go_see));
				if (dialog != null && !dialog.isShowing()) {
					dialog.show();
				}
				dialog.setOnClickLeftListener(new onClickLeftListener() {
					@Override
					public void onClickLeft() {
						dialog.dismiss();
						if(context instanceof OrderUnprocessed){
							context.finish();
						}
					}
				});
				dialog.setOnClickRightListener(new onClickRightListener() {

					@Override
					public void onClickRight() {
						dialog.dismiss();
						Intent intent = new Intent(context,
								StartServiceActivity.class);
						intent.putExtra(OrderMsg.extra_type, 1);
						intent.putExtra(OrderMsg.extra_order_id,order_id);
						if(context instanceof OrderUnprocessed){
							context.startActivityForResult(intent,OrderUnprocessed.code);
						}else{
							context.startActivity(intent);
						}
					}
				});
			} else if (result.equals(Value.MSG_RESULT_NOPASS)) {
				dialog.setContent(res.getString(R.string.msg_unprocessed_nopass));
				dialog.setLeftButtonText(res.getString(R.string.mydailog_know));
				dialog.setRightButtonText(res.getString(R.string.mydailog_go_see));
				if (dialog != null && !dialog.isShowing()) {
					dialog.show();
				}
				dialog.setOnClickLeftListener(new onClickLeftListener() {
					@Override
					public void onClickLeft() {
						dialog.dismiss();
						if(context instanceof OrderUnprocessed){
							context.finish();
						}
					}
				});
				dialog.setOnClickRightListener(new onClickRightListener() {

					@Override
					public void onClickRight() {

						dialog.dismiss();
						Intent intent=new Intent(context,OrderUnprocessed.class);
						intent.putExtra(ExamineMsg.EXAMINE_TYPE,ExamineMsg.TYPE_OF);
						intent.putExtra(ExamineMsg.EXAMINE_STATUS,ExamineMsg.STATUS_AGAIN);
						if(context instanceof OrderUnprocessed){
							context.startActivityForResult(intent,OrderUnprocessed.code);
						}else{
							context.startActivity(intent);
						}
					}
				});
			}

		}
	}
	public synchronized static void onRoundTripDialog(final Activity context,final Msgs.Data msgs_data,boolean isSingle) {
		if (msgs_data != null) {
		MyAlertDailog dialog = new MyAlertDailog(context);
		Resources res = context.getResources();
		dialog.setTitle(res.getString(R.string.mydailog_title));
		dialog.setContent(String.format(res.getString(R.string.msg_round_trip), msgs_data.getRound_trip().equals(Value.ROUND_TRIP)?
				res.getString(R.string.text_round_trip):res.getString(R.string.text_round_trip_no)));
		if(isSingle){
			dialog.setSingle(res.getString(R.string.mydailog_know));
			if (dialog != null && !dialog.isShowing()) {
				dialog.show();
			}
			dialog.setOnClickSingleListener(new onClickSingleListener() {
				
				@Override
				public void onClickSingle() {
					// TODO Auto-generated method stub
					
				}
			});
		}else{
			dialog.setLeftButtonText(res.getString(R.string.mydailog_know));
			dialog.setRightButtonText(res.getString(R.string.mydailog_see_detail));
			if (dialog != null && !dialog.isShowing()) {
				dialog.show();
			}
			dialog.setOnClickLeftListener(new onClickLeftListener() {
				@Override
				public void onClickLeft() {
					
				}
			});
			dialog.setOnClickRightListener(new onClickRightListener() {

				@Override
				public void onClickRight() {
					Intent intent = new Intent(context,
							StartServiceActivity.class);
					intent.putExtra(OrderMsg.extra_type, OrderMsg.TYPE_LIST);
					intent.putExtra(OrderMsg.extra_order_id,msgs_data.getOrder_id());
					context.startActivity(intent);
				}
			});
		}
		}
		
		
		
	}
	/**
	 * AA
	 */
	public synchronized static void onAADialog(final Activity context,
			final Msgs.Data msgs_data) {
		MyAlertDailog dailog = new MyAlertDailog(context);
		if (msgs_data != null && msgs_data.getContent() != null) {
			dailog.setHtml(msgs_data.getContent());
			dailog.setSingle(context.getString(R.string.mydailog_know));
			if (dailog != null && !dailog.isShowing()) {
				dailog.show();
			}
		}

	}

}
